<?php
namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\shop\model\ArtworkGroup as CategoryModel;
use think\Hook;

/**
 * 艺术家控制器
 */
class ArtworkGroup extends Admin
{
    public function index()
    {
    	//记录call_back
    	cookie('__forward__', $_SERVER['REQUEST_URI']);

        $data_list = CategoryModel::order('sort,id desc')->paginate();

        return ZBuilder::make('table')
            ->setPageTitle('艺术品分类') 
            ->setSearch(['id' => 'ID', 'title' => '头衔', 'name' => '姓名'])
            ->addColumns([
                ['id', 'ID'],
                ['title', '分类名称'],
                ['photo', '分类图标', 'picture'],
                ['create_time', '创建时间', 'datetime'],
                ['status', '状态', 'switch'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,enable,disable,delete')
            ->addRightButtons('edit,delete')
            ->setRowList($data_list)
            ->fetch(); // 渲染页面
    }

    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($cate = CategoryModel::create($data)) {
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }

        return ZBuilder::make('form')
            ->setPageTitle('新增分类')
            ->addFormItems([
                ['text', 'title', '分类名称'],
                ['image', 'photo', '图标'],
                ['text', 'sort', '排序'],
                ['radio', 'status', '状态', '', ['禁用', '启用'], 1],
            ])
            ->fetch();
    }

    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (CategoryModel::update($data)) {
                $user = CategoryModel::get($data['id']);
                $this->success('编辑成功', cookie('__forward__'));
            } else {
                $this->error('编辑失败');
            }
        }

        // 获取数据
        $info = CategoryModel::where('id', $id)->find();

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
            	['hidden', 'id'],
                ['text', 'title', '分类名称'],
                ['image', 'photo', '图标'],
                ['text', 'sort', '排序'],
                ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }

    public function delete($ids = [])
    {
        Hook::listen('artist_user_delete', $ids);
        return $this->setStatus('delete');
    }


    /**
     * 设置用户状态：删除、禁用、启用
     */
    public function setStatus($type = '', $record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $uid_delete = is_array($ids) ? '' : $ids;
        $ids        = array_map('get_nickname', (array)$ids);
        return parent::setStatus($type, ['user_'.$type, 'shop_artwork_group', $uid_delete, UID, implode('、', $ids)]);
    }
}

